<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="140px"
      v-loading="formLoading"
    >
      <el-form-item label="农舍" prop="houseId">
        <el-select v-model="formData.houseId" filterable :disabled="formType==='update'" placeholder="请选择农舍" clearable class="!w-240px" >
          <el-option
            v-for="houseIitem in props.houseList "
            :key="houseIitem.id"
            :label="houseIitem.houseName"
            :value="houseIitem.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="正常蛋总枚数" prop="normalNum">
        <el-input-number v-model="formData.normalNum" placeholder="请输入正常蛋总枚数" style="width:240px" />
      </el-form-item>
      <el-form-item label="正常蛋装箱规格" prop="normalSize">
        <el-input-number v-model="formData.normalSize" placeholder="请输入正常蛋装箱规格" style="width:240px"/>
      </el-form-item>
      <el-form-item label="双黄蛋总枚数" prop="doubleNum">
        <el-input-number v-model="formData.doubleNum" placeholder="请输入双黄蛋总枚数" style="width:240px"/>
      </el-form-item>
      <el-form-item label="双黄蛋装箱规格" prop="doubleSize">
        <el-input-number v-model="formData.doubleSize" placeholder="请输入双黄蛋装箱规格" style="width:240px"/>
      </el-form-item>
      <el-form-item label="鸡屎蛋总枚数" prop="shitNum">
        <el-input-number v-model="formData.shitNum" placeholder="请输入鸡屎蛋总枚数" style="width:240px"/>
      </el-form-item>
      <el-form-item label="鸡屎蛋装箱规格" prop="shitSize">
        <el-input-number v-model="formData.shitSize" placeholder="请输入鸡屎蛋装箱规格" style="width:240px"/>
      </el-form-item>
      <el-form-item label="血斑蛋总枚数" prop="bloodNum">
        <el-input-number v-model="formData.bloodNum" placeholder="请输入血斑蛋总枚数" style="width:240px"/>
      </el-form-item>
      <el-form-item label="烂蛋总枚数" prop="rottenNum">
        <el-input-number v-model="formData.rottenNum" placeholder="请输入烂蛋总枚数" style="width:240px"/>
      </el-form-item>
      <el-form-item label="液蛋总重量" prop="liquidNum">
        <el-input-number v-model="formData.liquidNum" placeholder="请输入液蛋总重量" style="width:240px"/>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入描述" />
      </el-form-item>

      <el-form-item label="产蛋日期" prop="eggTime">
        <el-date-picker
          v-model="formData.eggTime"
          clearable
          placeholder="请选择产蛋日期"
          type="date"
          value-format="x"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { EggApi, EggVO } from '@/api/farm/egg'

/** 产蛋 表单 */
defineOptions({ name: 'EggForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = reactive({
  id: undefined,
  houseId: undefined,
  houseName:undefined,
  normalNum: 0,
  normalSize: 360,
  doubleNum: 0,
  doubleSize: 360,
  shitNum: 0,
  shitSize: 360,
  bloodNum: 0,
  rottenNum: 0,
  liquidNum: 0,
  description: undefined,
  status: 0,
  eggTime:undefined //产蛋日期
})
const formRules = reactive({
  houseId: [{ required: true, message: '农舍不能为空', trigger: 'blur' }],
  normalNum: [{ required: true, message: '正常蛋总枚数不能为空', trigger: 'blur' }],
  normalSize: [{ required: true, message: '正常蛋装箱规格不能为空', trigger: 'blur' }],
  doubleNum: [{ required: true, message: '双黄蛋总枚数不能为空', trigger: 'blur' }],
  doubleSize: [{ required: true, message: '双黄蛋装箱规格不能为空', trigger: 'blur' }],
  shitNum: [{ required: true, message: '鸡屎蛋总枚数不能为空', trigger: 'blur' }],
  shitSize: [{ required: true, message: '鸡屎蛋装箱规格不能为空', trigger: 'blur' }],
  bloodNum: [{ required: true, message: '血斑蛋总枚数不能为空', trigger: 'blur' }],
  rottenNum: [{ required: true, message: '烂蛋总枚数不能为空', trigger: 'blur' }],
  liquidNum: [{ required: true, message: '液蛋总重量不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
  eggTime: [{ required: true, message: '产蛋日期不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      const eggData = await EggApi.getEgg(id)
      Object.assign(formData,eggData)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
// props传参
const props = defineProps({
  houseList:{
    type:Array,
    required:false
  }
})



/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData as unknown as EggVO
    if (formType.value === 'create') {
      await EggApi.createEgg(data)
      message.success(t('common.createSuccess'))
    } else {
      await EggApi.updateEgg(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  Object.assign(formData,{
    id: undefined,
    houseId: undefined,
    normalNum: 0,
    normalSize: 360,
    doubleNum: 0,
    doubleSize: 360,
    shitNum: 0,
    shitSize: 360,
    bloodNum: 0,
    rottenNum: 0,
    liquidNum: 0,
    description: undefined,
    status: 0,
    houseName:undefined
  })
  formRef.value?.resetFields()
}
</script>